package com.srxz.personnelsystem.system.service.impl;

import com.srxz.personnelsystem.system.entity.LoginInfo;
import com.srxz.personnelsystem.system.entity.UserInfo;
import com.srxz.personnelsystem.system.mapper.converter.LoginInfoMapper;
import com.srxz.personnelsystem.system.mapper.dto.LoginInfoDTO;
import com.srxz.personnelsystem.system.repository.LoginInfoRepository;
import com.srxz.personnelsystem.system.repository.UserInfoRepository;
import com.srxz.personnelsystem.system.service.LoginInfoService;
import com.srxz.personnelsystem.util.BackMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @ClassName: LoginInfoServiceImpl
 * @author: wgchao
 * @createTime: 2019/1/23 2:06 PM
 * @Description: TODO
 */
@Service
@Transactional
public class LoginInfoServiceImpl implements LoginInfoService {
    @Autowired
    private LoginInfoRepository loginInfoRepository;
    @Autowired
    private LoginInfoMapper loginInfoMapper;
    @Autowired
    private UserInfoRepository userInfoRepository;

    @Override
    public BackMessage search(String userId) {
        LoginInfo loginInfo = loginInfoRepository.findFirstByUserIdAndDeleteTimeIsNull(userId);
        if(loginInfo != null){
            LoginInfoDTO loginInfoDTO = loginInfoMapper.dto(loginInfo);
            return BackMessage.success().putData(loginInfoDTO);
        }
        return BackMessage.success();
    }

    @Override
    public BackMessage update(LoginInfoDTO loginInfoDTO){
        LoginInfo loginInfo = loginInfoMapper.entity(loginInfoDTO);
        loginInfo.setLastUpdateTime(new Date());
        //TODO 设置修改人

        loginInfo = loginInfoRepository.save(loginInfo);
        return BackMessage.success().putData(loginInfoMapper.dto(loginInfo));
    }

    @Override
    public BackMessage delete(LoginInfoDTO loginInfoDTO){
        LoginInfo loginInfo = loginInfoMapper.entity(loginInfoDTO);
        loginInfo.setLastUpdateTime(new Date());
        loginInfo.setDeleteTime(new Date());
        //TODO 设置修改人

        loginInfo = loginInfoRepository.save(loginInfo);
        return BackMessage.success().putData(loginInfoMapper.dto(loginInfo));
    }

    @Override
    public BackMessage save(LoginInfoDTO loginInfoDTO){
        LoginInfo loginInfo = loginInfoMapper.entity(loginInfoDTO);

        //获取用户信息
        UserInfo userInfo = userInfoRepository.getOne(loginInfo.getUserId());
        if(userInfo==null ){
            return BackMessage.error("用户不存在");
        }
//        if(!userInfo.getStatus() ){
//            return BackMessage.error("用户不可用");
//        }
        if(!userInfo.getLogin()){
            return BackMessage.error("用户没有登陆权限");
        }

        loginInfo.setCreateTime(new Date());
        //TODO 设置修改人

        loginInfo = loginInfoRepository.save(loginInfo);
        return BackMessage.success().putData(loginInfoMapper.dto(loginInfo));
    }


}
